﻿using System;
using System.Collections;
using System.Globalization;
using System.Windows.Forms;
using System.IO;


namespace TravWinControllerSpace
{
    public partial class TravWinController
    {
        private void sortDetailsColumn (int column, string columnTag)
        {
            if (details.ListViewItemSorter == null)
                details.ListViewItemSorter = new DetailWinSorter (column, columnTag);
            else
            {
                DetailWinSorter sorter = (DetailWinSorter) details.ListViewItemSorter;
                sorter.SetColumn (column, columnTag);
                details.Sort ();
            }
        }
    }

    public class DetailWinSorter : IComparer
    {
        private int column;
        private SortOrder order;
        private char sortType;

        public DetailWinSorter (int newColumn, string newColumnType)
        {
            SetNewColumn (newColumn, newColumnType);
        }

        private void SetNewColumn (int newColumn, string newColumnType)
        {
            order = SortOrder.Ascending;
            column = newColumn;
            if (string.IsNullOrEmpty (newColumnType))
                sortType = 'S';
            else
                sortType = Char.ToUpper (newColumnType[0]);
        }

        public void SetColumn (int newColumn, string newColumnType)
        {
            if (column == newColumn)
                order = order == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
            else
                SetNewColumn (newColumn, newColumnType);
        }


        public int Compare (object x, object y)
        {
            int result;

            ListViewItem itemX = (ListViewItem) x;
            ListViewItem itemY = (ListViewItem) y;

            if (column == TravWinController.ColumnType)
            {
                string extX = Path.GetExtension (itemX.SubItems[TravWinController.ColumnBaseName].Text);
                string extY = Path.GetExtension (itemY.SubItems[TravWinController.ColumnBaseName].Text);

                result = extX.CompareTo (extY);
            }
            else if (column == TravWinController.ColumnSelection)
                // Sort by selection state.
                result = itemY.Selected.CompareTo (itemX.Selected);
            else
            {
                string textX = itemX.SubItems[column].Text;
                string textY = itemY.SubItems[column].Text;

                if (sortType == 'N')
                {
                    long v1 = long.Parse (textX);
                    long v2 = long.Parse (textY);
                    result = v1.CompareTo (v2);
                }
                else if (sortType == 'D')
                {
                    DateTime v1 = DateTime.Parse (textX, CultureInfo.CurrentUICulture, DateTimeStyles.None);
                    DateTime v2 = DateTime.Parse (textY, CultureInfo.CurrentUICulture, DateTimeStyles.None);
                    result = v1.CompareTo (v2);
                }
                else
                    result = string.CompareOrdinal (textX, textY);
            }

            return order == SortOrder.Ascending ? result : -result;
        }
    }

}
